<template>
  <div class="login-container">
    <el-card class="login-card-my">
      <img src="@/assets/login.png" class="login-img" alt="">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <el-form-item prop="loginName">
          <span
            class="svg-container el-icon-mobile-phone"
          />
          <el-input
            v-model="loginForm.loginName"
            placeholder="请输入账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container el-icon-lock" />
          <el-input
            :key="passwordType"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-row type="flex">
          <el-col>
            <el-form-item prop="code">
              <span class="iconfont svg-container icon-24gl-shieldCheck" />
              <el-input
                v-model="loginForm.code"
                placeholder="请输入验证码"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <div class="imgCode" @click="getAuthCode()"> <img :src="codeSrc" alt=""></div>
          </el-col>
        </el-row>
        <el-button :loading="loading" type="primary" class="login-btn" @click="handleLogin">登录</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getAuthCode, userLogin, getUserInfo } from '@/api'
export default {
  name: 'Login',
  data() {
    return {
      codeSrc: '',
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        clientToken: '2oK1PaK4JmMWbFb27JVpDdFXYsoYuQJB',
        code: '',
        loginType: 0
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  // computed: {
  //   ...mapGetters('clientToken')
  // },
  watch: {
  },
  created() {
    this.getAuthCode()
  },
  mounted() {
  },
  methods: {
    // 获取验证码图片
    async getAuthCode() {
      const { data } = await getAuthCode(this.loginForm.clientToken)
      const windowUrl = window.URL || window.webkitURL // 处理浏览器兼容性
      this.codeSrc = windowUrl.createObjectURL(data)
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.loading = true
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            const { data } = await userLogin(this.loginForm)
            if (data.token !== null) {
              this.$store.commit('user/setUserToken', data.token)
              const res = await getUserInfo(data.userId)
              this.$store.commit('user/setUserInfo', res.data)
              this.$router.push('/')
              this.$message.success(data.msg)
            } else {
              this.$message.warning(data.msg)
            }
          } catch (error) {
            this.$message.warning(error)
          }
        } else {
          this.getAuthCode()
        }
      })
      this.loading = false
    }
  }
}
</script>

<style lang="scss" >
$bg:#283443;
$light_gray:#fff;
$cursor: #fff;
.login-card-my{
   padding: 0;
}
.imgCode{
  // position: relative;
  // left: -10px;
  width: 130px;
  height: 53px;
  img{
  height:53px;
    }
}
.login-img{
  position: absolute;
  top: -15%;
  left: 50%;
  transform: translateX(-50%);
  width: 96px;
  height: 96px;

}

.login-container {
  background-image: url('~@/assets/bglogin.jpg');
  .login-card-my {
   position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
    overflow: unset!important;
}
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .login-btn{
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
  }
}
.el-form-item__content{
  background-color: #fff!important;
  .el-input{
    background-color: #fff;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.el-icon-lock{
  font-size: 16px;
}
.el-icon-mobile-phone{
  font-size: 16px;
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;

  .login-form {
    position: relative;
    width: 446px;
    max-width: 100%;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    background-color: #fff!important;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
